package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlProcess;
import com.ruoyi.sl.service.ISlGprocessService;
import com.ruoyi.sl.service.ISlModuleService;
import com.ruoyi.sl.service.ISlWorkshopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlSpecial;
import com.ruoyi.sl.service.ISlSpecialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 特殊系数Controller
 * 
 * @author admin
 * @date 2024-06-10
 */
@RestController
@RequestMapping("/sl/special")
public class SlSpecialController extends BaseController
{
    @Autowired
    private ISlSpecialService slSpecialService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlWorkshopService slWorkshopService;

    @Autowired
    private ISlGprocessService slGprocessService;

    /**
     * 查询特殊系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:special:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlSpecial slSpecial)
    {
        startPage();
        List<SlSpecial> list = slSpecialService.selectSlSpecialList(slSpecial);
        return getDataTable(list);
    }

    /**
     * 导出特殊系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:special:export')")
    @Log(title = "特殊系数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlSpecial slSpecial)
    {
        List<SlSpecial> list = slSpecialService.selectSlSpecialList(slSpecial);
        ExcelUtil<SlSpecial> util = new ExcelUtil<SlSpecial>(SlSpecial.class);
        util.exportExcel(response, list, "特殊系数数据");
    }

    /**
     * 获取特殊系数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:special:query')")
    @GetMapping(value = { "/", "/{ssId}" })
    public AjaxResult getInfo(@PathVariable(value = "ssId", required = false) Long ssId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(ssId))
        {
            SlSpecial slSpecial = slSpecialService.selectSlSpecialBySsId(ssId);
            ajax.put(AjaxResult.DATA_TAG, slSpecial);
            //获取模块对应的车间
            if(StringUtils.isNotNull(slSpecial.getModuleId())){
                ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slSpecial.getModuleId()));
            }
            //获取车间对应的大工序
            if(StringUtils.isNotNull(slSpecial.getWorkshopId())){
                ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slSpecial.getWorkshopId()));
            }
        }
        return ajax;

        //return success(slSpecialService.selectSlSpecialBySsId(ssId));
    }

    /**
     * 根据用户名获取特殊系数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:special:query')")
    @GetMapping(value = {  "/user/{userName}" })
    public AjaxResult getUpdateInfo(@PathVariable("userName") String userName)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        SlSpecial slSpecial = slSpecialService.selectSlSpecialByUserNameForUpdate(userName);
        ajax.put(AjaxResult.DATA_TAG, slSpecial);
        //获取模块对应的车间
        if(StringUtils.isNotNull(slSpecial.getModuleId())){
            ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slSpecial.getModuleId()));
        }
        //获取车间对应的大工序
        if(StringUtils.isNotNull(slSpecial.getWorkshopId())){
            ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slSpecial.getWorkshopId()));
        }

        return ajax;

    }


    /**
     * 新增特殊系数
     */
    @PreAuthorize("@ss.hasPermi('sl:special:add')")
    @Log(title = "特殊系数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlSpecial slSpecial)
    {
        slSpecial.setCreateBy(getUsername());
        return toAjax(slSpecialService.insertSlSpecial(slSpecial));
    }

    /**
     * 修改特殊系数
     */
    @PreAuthorize("@ss.hasPermi('sl:special:edit')")
    @Log(title = "特殊系数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlSpecial slSpecial)
    {
        slSpecial.setUpdateBy(getUsername());
        return toAjax(slSpecialService.updateSlSpecial(slSpecial));
    }

    /**
     * 删除特殊系数
     */
    @PreAuthorize("@ss.hasPermi('sl:special:remove')")
    @Log(title = "特殊系数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ssIds}")
    public AjaxResult remove(@PathVariable Long[] ssIds)
    {
        return toAjax(slSpecialService.deleteSlSpecialBySsIds(ssIds));
    }
}
